<?php
/**
 * @author spenceryg
 * @date 2022/11/26
 * @time 10:17 下午
 * @focus love & life
 */


namespace app\auto\controller;


use app\auto\business\TableBusiness;
use app\auto\business\TemplateBusiness;
use app\auto\lib\ControllerGenerator;
use app\auto\lib\ModelGenerator;
use app\auto\model\A;
use app\auto\model\B;
use app\common\model\Department;
use app\common\model\Disease;
use app\common\model\Hospital;
use app\common\model\Position;
use think\facade\Request;
use think\facade\View;
use think\helper\Str;
use think\response\Json;

class Table extends \app\BaseController
{

    public function index()
    {
        $tables = \app\auto\model\Table::select();
        $tables->append(['controller_id'])
            ->withAttr('controller_id', function ($value, $data){
                $table_name = str_replace(env('DATABASE.PREFIX'),'', $data['name']);
                $controller_name = Str::studly($table_name);
                $controller_id = \app\auto\model\Controller::where('name', $controller_name)
                    ->where('app_id', 1)->value('id');
                return $controller_id ?: 0;
            });
        View::assign('data', $tables);
        $apps = \app\auto\model\App::select();
        View::assign('apps', $apps);
        return View::fetch();
    }


    public function add()
    {
        if (request()->isPost()) {
            $fields = ['name', 'comment','apps'];
            $data = input($fields);
            return success('添加成功', TableBusiness::add($data));
        } else {
            $apps = \app\auto\model\App::select();
            View::assign('apps', $apps);
            return View::fetch();
        }
    }

    /**
     * delete
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete(): Json
    {
        $id = input('id');
        TableBusiness::delete($id);
        return success('删除表成功', $id);
    }


    public function deleteAll()
    {
        TableBusiness::deleteAll();
        return success('清空表成功' );
    }

    /**
     * setSysField
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function setSysField(): Json
    {
        $data = input();
        TableBusiness::setSysField($data);
        return success('操作成功！');
    }


    /**
     * fastAdd
     * @return string|Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function fastAdd()
    {
        if (request()->isPost()) {
            $data = input();
            TableBusiness::fastAdd($data);
            return success();
        } else {
            $apps = \app\auto\model\App::select();
            View::assign('apps', $apps);
            return View::fetch();
        }
    }





    public function v6()
    {
        $tables = \app\auto\model\Table::select();
        $res = [];
        foreach ($tables as $table) {
            $this_data = [];
            $this_data['key'] = $table['comment'] . ' - ' . $table['name'];
            $items = [];
            $fields = \app\auto\model\Field::where('table_id', $table['id'])->select();
            foreach ($fields as $field) {
                $items[] = [
                    'name' => $field['comment'] . ' - ' . $field['name'],
                    'iskey' => false,
                    'figure' => "Hexagon",
                    'color' => '#afd4fe'
                ];
            }
            $this_data['items'] = $items;
            $res[] = $this_data;
        }
        View::assign('data', json_encode($res));

        $relations = \app\auto\model\Relation::select();
        $relation_arr = [];
        foreach ($relations as $relation) {
            $this_obj = [];
            $table_master = \app\auto\model\Table::find($relation['table_id_master']);
            $table_sliver = \app\auto\model\Table::find($relation['table_id_sliver']);
            $this_obj['from'] = $table_master['comment'] . ' - ' . $table_master['name'];
            $this_obj['to'] = $table_sliver['comment'] . ' - ' . $table_sliver['name'];
            $this_obj['text'] = '0..n';
            $this_obj['toText'] = '1';
            $relation_arr[] = $this_obj;
        }
        View::assign('relation', json_encode($relation_arr));
        return View::fetch();
    }


    /**
     * 生成代码
     * generate
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function generate(): Json
    {

        ModelGenerator::generate();
        ControllerGenerator::beforeGenerate();
        ControllerGenerator::generate();
        ControllerGenerator::afterGenerate();
        return success();
    }



}
